﻿Shader "Kun URP/Post Process/RadialBlur"
{
    Properties
    {
        [HideInInspector]_MainTex("MainTex",2D)="white"{}
    }
    SubShader
    {
        Tags{
            "RenderPipeline"="UniversalRenderPipeline"
        }
        Cull Off ZWrite Off ZTest Always
        HLSLINCLUDE
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
        CBUFFER_START(UnityPerMaterial)
            float _BlurRadius;
            float _CenterX;
            float _CenterY;
            float _Instensity;
            int _LoopCount;
            float4 _MainTex_TexelSize;
        CBUFFER_END
        TEXTURE2D( _MainTex);
        SAMPLER(sampler_MainTex);
        TEXTURE2D(_SourceTex);
        SAMPLER(sampler_SourceTex);
        struct Attributes
        {
            float4 positionOS           : POSITION;
            float2 uv                   : TEXCOORD;
        };
        struct Varyings
        {
            float4 positionCS           : SV_POSITION;
            float2 uv                   : TEXCOORD;
        };
        ENDHLSL
        pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            Varyings vert(Attributes input)
            {
                Varyings output;
                output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
                output.uv = input.uv;
                return output;
            }
            half4 frag(Varyings input) : SV_TARGET
            {
                float4 col = 0;
                float2 dir = (input.uv - float2(_CenterX, _CenterY)) * _BlurRadius * 0.01;
                for (int i = 0; i < _LoopCount; i++)
                {
                    col += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv + dir * i) / _LoopCount;
                }
                return col;
            }
            ENDHLSL
        }
        pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            Varyings vert(Attributes input)
            {
                Varyings output;
                output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
                output.uv = input.uv;
                return output;
            }
            half4 frag(Varyings input) : SV_TARGET
            {
                float4 blur = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
                float4 source = SAMPLE_TEXTURE2D(_SourceTex, sampler_SourceTex, input.uv);
                return lerp(source, blur, _Instensity);
            }
            ENDHLSL
        }
    }
}
